package com.dream.xiaobo.service.impl;

import com.dream.xiaobo.dao.impl.UserDaoImpl;
import com.dream.xiaobo.entity.User;
import com.dream.xiaobo.service.UserService;
import com.dream.xiaobo.utils.Constants;
import com.dream.xiaobo.utils.EmailUtils;
import com.dream.xiaobo.utils.MD5Utils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author xiaobo
 * @date 2021/8/27 - 21:38
 */
public class UserServiceImpl implements UserService {

    //创建UserDao实例化对象
    private UserDaoImpl userDao = new UserDaoImpl();;

    /**
     * 检查用户是否存在的逻辑接口实现方法
     * @param username
     * @return
     * @throws SQLException
     */
    @Override
    public boolean checkUser(String username) throws SQLException {

        //调用根据用户名查询信息方法
        User user = userDao.selectUserByName(username);
        //查询结果不为空时
        if (null != user) {
            //返回true
            return true;
        }
        //返回false
        return false;
    }

    /**
     * 注册逻辑接口实现方法
     * @param user
     * @return
     */
    @Override
    public Integer rigistUser(User user) throws SQLException {

        //调用添加信息方法
        Integer row = userDao.insertUser(user);
        //发送一封邮件
        EmailUtils.sendEmail(user);
        //返回结果
        return row;
    }
    /**
     * 激活逻辑方法
     * @param c
     * @return
     * @throws SQLException
     */
    @Override
    public Integer activeUser(String c) throws SQLException {

        //调用根据激活码查询信息结果
        User user = userDao.selectUserByCode(c);
        //当查询结果不为空时
        if(null == user){
            //返回激活失败信息   0
            return Constants.ACTIVE_FAIL;
        }
        //当激活状态为1时
        if(user.getuStatus().equals(Constants.USER_ACTIVE)){
            //返回激活成功  1
            return Constants.ACTIVE_ALREADY;
        }
        //调用更新激活状态方法
        Integer i = userDao.updateByUser(user.getuId());
        //如果有记录、说明更新成功
        if(i >= 1){
            //返回激活成功状态 1
            return Constants.ACTIVE_SUCCESS;
        }
        //返回激活失败
        return Constants.ACTIVE_FAIL;
    }
    /**
     * 登录逻辑方法
     * @param username
     * @param password
     * @return
     * @throws SQLException
     */
    @Override
    public User loginUser(String username, String password) throws SQLException {
        //将密码用MD5加密
        String md5Password = MD5Utils.md5(password);
        //调用根据用户名查询信息的方法
        User user = userDao.selectUserByName(username);
        //当查询到信息时和查询到的密码和输入的密码一致时
        if(null != user && user.getuPassword().equals(md5Password)){
            //返回信息
            return user;
        }
        //返回空
        return null;
    }

    @Override
    public List<User> checkRoleUser() throws SQLException {

        List<User> list = userDao.selectUserByRole();

        return list;
    }

    @Override
    public void deleteUser(String uId) throws SQLException {

        userDao.deleteUserByUId(uId);

    }

    @Override
    public List<User> checkStatusUser() throws SQLException {

        List<User> list = userDao.selectUserByStatus();

        return list;
    }
}
